package com.qubuyer.business.auth.viewmodel

import androidx.lifecycle.MutableLiveData
import com.qubuyer.business.auth.model.AuthModel
import com.qubuyer.constant.AppConstant
import com.qubuyer.core.anno.AutoInject
import com.qubuyer.core.framework.BaseViewModel
import com.qubuyer.core.http.RetrofitCall
import com.qubuyer.repository.entitys.LoginEntity

/**
 * Author: linwg
 * Time: 2021/3/4 0004
 * Description:
 */
class BindingViewModel : BaseViewModel() {

    val getCodeResult = MutableLiveData<Boolean>()
    val thirdPartLoginResult = MutableLiveData<Int>()
    var token :String? = null
    @AutoInject
    private val authModel = AuthModel()

    fun bindPhone(phone: String, code: String, openId: String?, platform: String) {
        RetrofitCall<LoginEntity?>(lifecycleOwner, this) {
            api { authModel.bindPhone(phone, code, openId ?: "", platform) }
            success {
                thirdPartLoginResult.value = 200
            }
            fail { data, code, _ ->
                if(code == AppConstant.CODE_GO_SET_PWD){
                    token = data?.token
                }
                thirdPartLoginResult.value = code
            }
        }
    }

    fun getCode(phone: String) {
        RetrofitCall<String?>(lifecycleOwner, this) {
            api { authModel.getCode(phone) }
            success {
                getCodeResult.value = true
            }
            fail { _, _, _ ->
                getCodeResult.value = false
            }
        }
    }
}